package com.tangyee.home_blood_center.fragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.tangyee.home_blood_center.bean.BSRecord;
import com.tangyee.home_blood_center.db.BSRecordHolder;
import com.tangyee.home_blood_center.view.BSRecordRowView;
import com.tangyee.home_blood_center.view.CircleView;
import com.tangyee.R;
import com.tangyee.home_blood_center.bean.BSRecord;
import com.tangyee.home_blood_center.db.BSRecordHolder;
import com.tangyee.home_blood_center.interfaces.OnBSRecordClickListener;
import com.tangyee.home_blood_center.view.BSRecordRowView;
import com.tangyee.home_blood_center.view.CircleView;

import java.util.ArrayList;
import java.util.List;

/**
 * 7天血糖信息的表格，和BloodPressureFragment差不多
 */
public class BloodSugarFragment7days extends BaseFragment {

    private List<BSRecord> mBSRecords;
    private List<BSRecordRowView> mBSRecordRowViews;
    private CircleView mCircleView1;
    private CircleView mCircleView2;
    private CircleView mCircleView3;
    private int low;
    private int good;
    private int high;

    private int count = 7;

    public static BloodSugarFragment7days newInstance(String text){
        Bundle bundle = new Bundle();
        bundle.putString("text",text);
        BloodSugarFragment7days bloodSugarFragment7days = new BloodSugarFragment7days();
        bloodSugarFragment7days.setArguments(bundle);
        return  bloodSugarFragment7days;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_blood_sugar_7days, container, false);

        LinearLayout linearLayout = view.findViewById(R.id.record_table_7days);
        mBSRecordRowViews = new ArrayList<>();
        mBSRecords = BSRecordHolder.get(getContext()).getBSRecords(count);
        low = good = high = 0;
        for (int i = 0; i < count; i ++) {
            // 统计信息
            final BSRecord record = mBSRecords.get(i);
            analyzeData(record.getDatas());

            // 构建BSRecordRow
            BSRecordRowView bsRecordRowView = new BSRecordRowView(getContext());
            bsRecordRowView.setBSRecord(record);
            bsRecordRowView.setOnBSRecordClickListener(new OnBSRecordClickListener() {
                @Override
                public void OnTextView1Click(BSRecord bsRecord) {
                    Long sign = bsRecord.getDate().getTime() * 10 + 1;
                    startAddDataFragment(sign);
                }

                @Override
                public void OnTextView2Click(BSRecord bsRecord) {
                    Long sign = bsRecord.getDate().getTime() * 10 + 2;
                    startAddDataFragment(sign);
                }

                @Override
                public void OnTextView3Click(BSRecord bsRecord) {
                    Long sign = bsRecord.getDate().getTime() * 10 + 3;
                    startAddDataFragment(sign);
                }

                @Override
                public void OnTextView4Click(BSRecord bsRecord) {
                    Long sign = bsRecord.getDate().getTime() * 10 + 4;
                    startAddDataFragment(sign);
                }

                @Override
                public void OnTextView5Click(BSRecord bsRecord) {
                    Long sign = bsRecord.getDate().getTime() * 10 + 5;
                    startAddDataFragment(sign);
                }

                @Override
                public void OnTextView6Click(BSRecord bsRecord) {
                    Long sign = bsRecord.getDate().getTime() * 10 + 6;
                    startAddDataFragment(sign);
                }

                @Override
                public void OnTextView7Click(BSRecord bsRecord) {
                    Long sign = bsRecord.getDate().getTime() * 10 + 7;
                    startAddDataFragment(sign);
                }

                @Override
                public void OnTextView8Click(BSRecord bsRecord) {
                    Long sign = bsRecord.getDate().getTime() * 10 + 8;
                    startAddDataFragment(sign);
                }
            });
            mBSRecordRowViews.add(bsRecordRowView);
            linearLayout.addView(bsRecordRowView, 0);
        }

        mCircleView1 = view.findViewById(R.id.blood_info_left_7);
        mCircleView2 = view.findViewById(R.id.blood_info_middle_7);
        mCircleView3 = view.findViewById(R.id.blood_info_right_7);
        int total = low + good + high;
        mCircleView1.setDate(high, total);
        mCircleView2.setDate(good, total);
        mCircleView3.setDate(low, total);

        return view;

    }

    private void startAddDataFragment(Long sign) {
        AddBloodSugarRecordFragment addBloodSugarRecordFragment = AddBloodSugarRecordFragment.newInstance(sign);
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        List<Fragment> fragments = getFragmentManager().getFragments();
        for (int i = 0; i < fragments.size(); i ++) {
            transaction.hide(fragments.get(i));
        }
        transaction.setCustomAnimations(R.anim.move_right_in_activity, R.anim.move_right_out_activity, R.anim.move_right_out_activity, R.anim.move_left_out_activity) // 瞎设置
                .add(R.id.fragment_container, addBloodSugarRecordFragment, "addBloodSugarRecordFragment")
                .addToBackStack("addBloodSugarRecordFragment")
                .commit();
    }

    private void analyzeData(double ...datas) {
        for (double data: datas) {
            if (data <= 0) {
                continue;
            }
            if (data < 4.4) {
                low ++;
                continue;
            }
            if (data > 10.0) {
                high ++;
                continue;
            }
            good ++;
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && mBSRecordRowViews != null) {
            //相当于Fragment的onResume
            updateUI();
        } else {
            //相当于Fragment的onPause
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        // 坑啊！
        updateUI();
    }

    private void updateUI() {
        low = high = good = 0;
        mBSRecords = BSRecordHolder.get(getContext()).getBSRecords(count);
        for (int i = 0; i < count; i ++) {
            // 统计信息
            final BSRecord record = mBSRecords.get(i);
            analyzeData(record.getDatas());

            // 重设数据
            mBSRecordRowViews.get(i).setBSRecord(record);
        }
        int total = low + good + high;
        mCircleView1.setDate(high, total);
        mCircleView2.setDate(good, total);
        mCircleView3.setDate(low, total);
    }
}
